package com.baozhun.mall.common.base

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.BR
import com.baozhun.mall.common.R
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.ext.dismissLoadingExt
import com.baozhun.mall.common.ext.hideSoftKeyboard
import com.baozhun.mall.common.ext.showLoadingExt
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.listener.ViewOnClickListener
import com.baozhun.mall.common.model.enums.MainNavigateType
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.jaeger.library.StatusBarUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

/**
 * 作者　: hegaojian
 * 时间　: 2019/12/21
 * 描述　: 你项目中的Fragment基类，在这里实现显示弹窗，吐司，还有自己的需求操作 ，如果不想用Databind，请继承
 * BaseVmFragment例如
 * abstract class BaseFragment<VM : BaseViewModel> : BaseVmFragment<VM>() {
 */
abstract class BaseFragment<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDbFragment<VM, DB>(),
    View.OnClickListener,
    ViewOnClickListener,
    OnTitleBarListener {

    protected var mNoMoreData: Boolean = false

    abstract override fun initView(savedInstanceState: Bundle?)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isImmersiveNavigationBar()) {
            StatusBarUtil.setTransparent(requireActivity())
            StatusBarUtil.setTranslucentForImageViewInFragment(requireActivity(), null)
        }
    }

    /**
     * viewModel 对应的变量，默认为viewModel
     */
    override fun bindingVmVariable(): Int {
        return BR.viewModel
    }

    /**
     * viewModel 对应的变量，默认为viewModel
     */
    override fun bindingListenerVariable(): Int {
        return BR.listener
    }

    /**
     * 绑定xml变量
     */
    override fun bindingVariable() {
        mDataBinding?.setVariable(bindingVmVariable(), mViewModel)
        mDataBinding?.setVariable(bindingListenerVariable(), this)
        mDataBinding?.executePendingBindings()
        var titleBar = mDataBinding?.root?.findViewById<TitleBar>(R.id.title_bar)
        titleBar?.setOnTitleBarListener(this)
    }

    override fun onClick(v: View?) {

    }

    override fun onLeftClick(titleBar: TitleBar?) {
        super.onLeftClick(titleBar)
        when (titleBar?.leftIconAction) {
            TitleBar.ACTION_ICON_CLICK_MENU -> ARouter.getInstance().build(RoutePath.Common.SWIPE)
                .withTransition(R.anim.activity_open_enter, R.anim.activity_open_exit).navigation(activity)

            else -> {
                titleBar?.leftView?.id = com.hjq.bar.R.id.title_bar_left_view
                onClick(titleBar?.leftView)
            }
        }
    }

    override fun onLeft2Click(titleBar: TitleBar?) {
        super.onLeft2Click(titleBar)
        when (titleBar?.leftIcon2Action) {
            TitleBar.ACTION_ICON_CLICK_MENU -> ARouter.getInstance().build(RoutePath.Common.SWIPE)
                .withTransition(R.anim.activity_open_enter, R.anim.activity_open_exit).navigation(activity)

            else -> {
                titleBar?.leftView2?.id = com.hjq.bar.R.id.title_bar_left_view2
                onClick(titleBar?.leftView2)
            }
        }
    }

    override fun onRightClick(titleBar: TitleBar?) {
        super.onRightClick(titleBar)
        when (titleBar?.rightIconAction) {
            TitleBar.ACTION_ICON_CLICK_SEARCH -> BaseApplication.mEventViewModelInstance.switchTabEvent.value =
                MainNavigateType.values().indexOf(MainNavigateType.MESSAGE)

            TitleBar.ACTION_ICON_CLICK_MESSAGE -> ARouter.getInstance().build(RoutePath.Home.MESSAGE_CENTER).navigation()
            TitleBar.ACTION_ICON_CLICK_SHARE -> showToast("BaseFragment ->分享")
            else -> {
                titleBar?.rightView?.id = com.hjq.bar.R.id.title_bar_right_view
                onClick(titleBar?.rightView)
            }
        }
    }

    override fun onRight2Click(titleBar: TitleBar?) {
        super.onRightClick(titleBar)
        when (titleBar?.rightIcon2Action) {
            TitleBar.ACTION_ICON_CLICK_SEARCH -> BaseApplication.mEventViewModelInstance.switchTabEvent.value =
                MainNavigateType.values().indexOf(MainNavigateType.MESSAGE)

            TitleBar.ACTION_ICON_CLICK_MESSAGE -> ARouter.getInstance().build(RoutePath.Home.MESSAGE_CENTER).navigation()
            TitleBar.ACTION_ICON_CLICK_SHARE -> showToast("BaseFragment ->分享")
            else -> {
                titleBar?.rightView2?.id = com.hjq.bar.R.id.title_bar_right_view2
                onClick(titleBar?.rightView2)
            }
        }
    }

    /**
     * 懒加载 只有当前fragment视图显示时才会触发该方法
     */
    override fun lazyLoadData() {}

    /**
     * 创建LiveData观察者 Fragment执行onViewCreated后触发
     */
    override fun createObserver() {}

    /**
     * Fragment执行onViewCreated后触发
     */
    override fun initData() {

    }

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    override fun onPause() {
        super.onPause()
        hideSoftKeyboard(activity)
    }

    /**
     * 延迟加载 防止 切换动画还没执行完毕时数据就已经加载好了，这时页面会有渲染卡顿  bug
     * 这里传入你想要延迟的时间，延迟时间可以设置比转场动画时间长一点 单位： 毫秒
     * 不传默认 300毫秒
     * @return Long
     */
    override fun lazyLoadTime(): Long {
        return 300
    }

    /**
     * 是否显示沉浸式导航栏
     */
    protected open fun isImmersiveNavigationBar(): Boolean {
        return false
    }

    /**
     * 方便特殊场景（fragment里嵌套fragment）刷新时重写方法
     */
    open fun onRefresh(refreshLayout: RefreshLayout) {

    }

    /**
     * 方便特殊场景（fragment里嵌套fragment）刷新时透传RefreshLayout
     */
    open fun setRefreshLayout(refreshLayout: RefreshLayout) {

    }

    /**
     * 方便特殊场景（fragment里嵌套fragment）加载更多数据时重写方法
     */
    open fun onLoadMore(refreshLayout: RefreshLayout) {

    }

    open fun isNoMoreDate(): Boolean {
        return mNoMoreData
    }
}